package com.nuoniu.sibanyun.controller.erp.in;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.InInventoryPageSearchDto;
import com.nuoniu.sibanyun.entity.dto.InInventorySaveDto;
import com.nuoniu.sibanyun.entity.dto.InInventorySearchDto;
import com.nuoniu.sibanyun.entity.erp.in.InInventory;
import com.nuoniu.sibanyun.service.crm.ICrmCustomerService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryService;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 物料档案 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/in/inventory")
@Api(value = "物料档案管理", tags = "物料档案管理")
public class InInventoryController extends BaseController {

    @Autowired
    private IInInventoryService inventoryService;
    @Autowired
    private ICrmCustomerService customerService;

    /**
     * 获取档案列表
     * @author 小啊刚
     * @date 2020-09-25
     */
    @GetMapping("pageList")
    @ApiOperation("获取档案列表")
    public HttpResult pageList(Page page, InInventoryPageSearchDto inInventoryPageSearchDto) {
        return HttpResult.ok(inventoryService.pageList(page, inInventoryPageSearchDto));
    }

    /**
     * 搜索匹配物料列表
     * @author 小啊刚
     * @date 2020-11-28
     */
    @GetMapping("getList")
    @ApiOperation("搜索匹配物料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "搜索关键字"),
    })
    public HttpResult getList(Page page,InInventorySearchDto inInventorySearchDto) {
        return HttpResult.ok(inventoryService.getList(page,inInventorySearchDto));
    }

    /**
     * 保存物料档案
     * @author 小啊刚
     * @date 2020-09-25
     */
    @PostMapping("save")
    @ApiOperation("保存物料档案")
    public HttpResult save(@RequestBody InInventorySaveDto inInventorySaveDto) {
        Long id = inventoryService.saveInInventory(inInventorySaveDto);
        if(inInventorySaveDto.getInInventory().getId() != null) {  //异步修改单据物料信息
            inventoryService.updateBillInventory(inInventorySaveDto.getInInventory());
        }
        return HttpResult.ok(id);
    }

    /**
     * 删除物料档案
     * @author 小啊刚
     * @date 2020-09-25
     */
    @PostMapping("delete")
    @ApiOperation("删除物料档案 ")
    public HttpResult delete(@RequestBody List<Integer> ids) {
        inventoryService.deleteByIds(ids);
        return HttpResult.ok();
    }

    @GetMapping("getInventoryQuantity")
    @ApiOperation("获取 某些物料档案 的可用量 和 现存量")
    public HttpResult getList(Set<Integer> inventoryIdList) {
        return HttpResult.ok(inventoryService.getInventoryQuantity(inventoryIdList));
    }

    @ApiOperation("导入数据")
    @PostMapping("importData")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "excel文件"),
            @ApiImplicitParam(name = "excelFiled", value = "excel字段列表"),
            @ApiImplicitParam(name = "dbFiled", value = "匹配好的字段列表"),
            @ApiImplicitParam(name = "classId", value = "物料分类id"),
    })
    public HttpResult importData(MultipartFile file, String[] excelFiled, String[] dbFiled, Integer classId) throws IOException {
        if (file == null) {
            throw new NuoNiuException("请选择导入的数据文件！");
        }

        if(classId == null) {
            throw new NuoNiuException("请选择物料分类");
        }

        Map result = inventoryService.importData(file, false, excelFiled, dbFiled, classId);
        return HttpResult.ok(result);
    }

    @ApiOperation("下载导入数据模板")
    @GetMapping(value = "importTemplate")
    public void importTemplate(HttpServletResponse response) {
        inventoryService.importTemplate(response);
    }

    @ApiOperation("解析Excel导入字段")
    @PostMapping("getExcelFiled")
    public HttpResult getExcelFiled(MultipartFile file) {
        if (file == null) {
            throw new NuoNiuException("请选择导入的数据文件！");
        }

        Map data = new HashMap();
        List<String> excelFiled = customerService.getExcelFiled(file);
        //获取存货字段
        List<Map<String, String>> inFiled = inventoryService.getInventoryImportFiled();
        data.put("excelFiled", excelFiled);
        data.put("inFiled", inFiled);
        return HttpResult.ok(data);
    }

    @ApiOperation("导出物料档案")
    @GetMapping("exportData")
    public void exportData(Integer classId, HttpServletResponse response) {
        inventoryService.exportData(classId, response);
    }

    @ApiOperation("根据编码获取存货")
    @GetMapping("getByCode")
    public HttpResult getByCode(String code) {
        InInventory  inventory = new InInventory();
        inventory.setInventoryCode(code);
        inventory.setCompanyId(UserThreadLocal.get().getCompanyId());
        return HttpResult.ok(inventoryService.getByCode(inventory));
    }

    @ApiOperation("获取商品属性")
    @GetMapping("getCommodityAttribute")
    public HttpResult getCommodityAttribute(Long id) {
        return HttpResult.ok(inventoryService.getCommodityAttribute(id));
    }

    @GetMapping("dAllOrder")
    public void dAllOrder() {
        inventoryService.dAllOrder();
    }

 }
